package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.*;
import com.woniuxy.controller.info.BrandPageInfo;
import com.woniuxy.controller.info.GetBrandByCatCodeInfo;
import com.woniuxy.controller.info.GetOneBrandInfo;
import com.woniuxy.dao.PawnBrandCatRelDao;
import com.woniuxy.dao.PawnGoodsDao;
import com.woniuxy.dao.PawnProductCatDao;
import com.woniuxy.exception.*;
import com.woniuxy.model.PawnBrand;
import com.woniuxy.dao.PawnBrandDao;
import com.woniuxy.model.PawnBrandCatRel;
import com.woniuxy.model.PawnGoods;
import com.woniuxy.model.PawnProductCat;
import com.woniuxy.service.PawnBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Service
public class PawnBrandServiceImpl extends ServiceImpl<PawnBrandDao, PawnBrand> implements PawnBrandService {

    /**
     * 品牌分类关联表
     */
    @Resource
    private PawnBrandCatRelDao pbcrd;
    /**
     * 分类表
     */
    @Resource
    private PawnProductCatDao ppcd;

    @Resource
    private PawnGoodsDao pgd;
    /**
     * 查询所有品牌
     * @param bpf 分页参数
     * @return BrandPageInfo
     */
    @Override
    public Result getAllBrand(BrandPageForm bpf) {
        Page<PawnBrand> brandPageForm = baseMapper.selectPage(bpf, null);
        Page<BrandPageInfo> brandPageInfoPage = new Page<>();
        List<BrandPageInfo> brandPageInfos = NewCopyBean.copyList(brandPageForm.getRecords(), BrandPageInfo.class);
        brandPageInfoPage.setRecords(brandPageInfos);
        brandPageInfoPage.setCurrent(brandPageForm.getCurrent());
        brandPageInfoPage.setSize(brandPageForm.getSize());
        brandPageInfoPage.setTotal(brandPageForm.getTotal());
        return Result.success(200,"请求成功",brandPageInfoPage);
    }


    /**
     * 根据品牌名或品牌首字母模糊查询
     * @param bpf str
     * @return BrandPageInfo
     */
    @Override
    public Result getBrandByLike(BrandPageForm bpf) throws DdException {
        if(bpf.getStr()!=""||bpf.getStr()!=null){
            QueryWrapper<PawnBrand> wrapper = new QueryWrapper<PawnBrand>().like("brand_name", bpf.getStr()).or().like("f_letter",bpf.getStr());
            Page<PawnBrand> brandPageForm = baseMapper.selectPage(bpf, wrapper);
            if(ObjectUtils.isEmpty(brandPageForm.getRecords())){
                throw new ProductCatNotExsitsException(50001,"未查询到分类");
            }else {
                Page<BrandPageInfo> brandPageInfoPage = new Page<>();
                List<BrandPageInfo> brandPageInfos = NewCopyBean.copyList(brandPageForm.getRecords(), BrandPageInfo.class);
                brandPageInfoPage.setRecords(brandPageInfos);
                brandPageInfoPage.setSize(brandPageForm.getSize());
                brandPageInfoPage.setCurrent(brandPageForm.getCurrent());
                brandPageInfoPage.setTotal(brandPageForm.getTotal());
                return Result.success(200,"查询成功",brandPageInfoPage);
            }
        }else{
            throw new ProductCatNotExsitsException(50001,"未输入查询信息");
        }
    }

    /**
     * 根据选中的删除
     * @param dbf ischecked
     * @return data为null
     */
    @Override
    @Transactional
    public Result deleteBrand(DeleteBrandForm dbf) throws DdException{
        List<BrandPageInfo> ischecked = dbf.getIschecked();
        if(ObjectUtils.isEmpty(ischecked)){
            throw new CheckedIsNullException(50009,"请勾选后删除");
        }else{
            for(BrandPageInfo b : ischecked){
                List<PawnGoods> brand_code = pgd.selectList(new QueryWrapper<PawnGoods>().eq("brand_code", b.getBrandCode()));
                if(!ObjectUtils.isEmpty(brand_code)){
                    throw new ProductCatNameExsitsException(50002,"该品牌下已有商品存在");
                }
            }
            for(BrandPageInfo b : ischecked){
                //先删Brand表
                baseMapper.deleteById(b.getBrandCode());
                //再删关联表
                pbcrd.delete(new QueryWrapper<PawnBrandCatRel>().eq("brand_code",b.getBrandCode()));
            }
            return Result.success(200,"删除成功",null);
        }
    }

    /**
     * 根据brandCode查询
     * @param gobf brandCode
     * @return GetOneBrandInfo
     */
    @Override
    public Result getBrandByCode(GetOneBrandForm gobf) {
        PawnBrand pawnBrand = baseMapper.selectById(gobf.getBrandCode());
        //查询出适用的商品大类
        List<PawnBrandCatRel> catBy = pbcrd.selectList(new QueryWrapper<PawnBrandCatRel>().eq("brand_code", gobf.getBrandCode()));
        List<String> strings = new ArrayList<>();
        for(PawnBrandCatRel p :catBy){
            PawnProductCat pawnProductCat = ppcd.selectById(p.getCatCode());
            strings.add(pawnProductCat.getCatName());
        }
        List<String> strs = new ArrayList<>();
        //查询出所有商品大类
        List<PawnProductCat> allcat = ppcd.selectList(new QueryWrapper<PawnProductCat>().eq("p_cat_code", "0"));
        for(PawnProductCat p :allcat){
            strs.add(p.getCatName());
        }
        GetOneBrandInfo getOneBrandInfo = NewCopyBean.copyOne(pawnBrand, GetOneBrandInfo.class);
        getOneBrandInfo.setPcatNames(strings);
        getOneBrandInfo.setAllPCatNames(strs);
        return Result.success(200,"请求成功",getOneBrandInfo);
    }

    /**
     * 修改品牌
     * @param ubf UpdateBrandForm
     * @return data为null
     */
    @Override
    @Transactional
    public Result updateBrand(UpdateBrandForm ubf) throws DdException{
        List<String> pcatNames = ubf.getPcatNames();
        if(pcatNames.size()==0){
            throw new BrandCatNotExsitsException(50011,"必须指定适用大类");
        }else{
            PawnBrand pawnBrand = NewCopyBean.copyOne(ubf, PawnBrand.class);
            pawnBrand.setFLetter(ubf.getFletter());
            baseMapper.updateById(pawnBrand);
            //修改关联表
            pbcrd.delete(new QueryWrapper<PawnBrandCatRel>().eq("brand_code",ubf.getBrandCode()));
            for(String s :pcatNames){
                PawnProductCat cat = ppcd.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_name", s));
                PawnBrandCatRel pawnBrandCatRel = new PawnBrandCatRel();
                pawnBrandCatRel.setBrandCode(ubf.getBrandCode());
                pawnBrandCatRel.setCatCode(cat.getCatCode());
                pbcrd.insert(pawnBrandCatRel);
            }
            return Result.success(200,"修改成功",null);
        }
    }

    /**
     * 新增品牌
     * @param sbf SaveBrandForm
     * @return data为null
     */
    @Override
    @Transactional
    public Result saveBrand(SaveBrandForm sbf) throws DdException{
        List<String> pcatNames = sbf.getPcatNames();
        if(pcatNames.size()==0){
            throw new BrandCatNotExsitsException(50011,"必须指定适用大类");
        }else{
            PawnBrand pawnBrand = NewCopyBean.copyOne(sbf, PawnBrand.class);
            pawnBrand.setFLetter(sbf.getFletter());
            baseMapper.insert(pawnBrand);
            PawnBrand brand = baseMapper.selectOne(new QueryWrapper<PawnBrand>().eq("brand_name", sbf.getBrandName()));
            //添加关联表
            for(String s : pcatNames){
                PawnProductCat cat = ppcd.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_name", s));
                PawnBrandCatRel pawnBrandCatRel = new PawnBrandCatRel();
                pawnBrandCatRel.setCatCode(cat.getCatCode());
                pawnBrandCatRel.setBrandCode(brand.getBrandCode());
                pbcrd.insert(pawnBrandCatRel);
            }
            return Result.success(200,"新增成功",null);
        }
    }


    /***
     * 根据大类code查询对应的所有品牌
     * @param gbbccf
     * @return
     */
    @Override
    public Result getBrandByCatCode(GetBrandByCatCodeForm gbbccf) {
        List<PawnBrandCatRel> brandCatRelList = pbcrd.selectList(new QueryWrapper<PawnBrandCatRel>().eq("cat_code", gbbccf.getCatCode()));
        List<GetBrandByCatCodeInfo> getBrandByCatCodeInfos = new ArrayList<>();
        for(PawnBrandCatRel p : brandCatRelList){
            PawnBrand pawnBrand = baseMapper.selectById(p.getBrandCode());
            GetBrandByCatCodeInfo getBrandByCatCodeInfo = NewCopyBean.copyOne(pawnBrand, GetBrandByCatCodeInfo.class);
            getBrandByCatCodeInfos.add(getBrandByCatCodeInfo);
        }
        return Result.success(200,"请求成功",getBrandByCatCodeInfos);
    }
}
